<?php

/**
 * articles actions.
 *
 * @package    blog
 * @subpackage articles
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class sfBlogArticlesActions extends sfActions
{
	
	 public function executeSearch(sfWebRequest $request)
  {
    /*$this->forwardUnless($query = $request->getParameter('query'), 'sfBlogArticles', 'index');
 
    $this->articles = Doctrine_Core::getTable('BlogArticle') ->getForLuceneQuery($query);
    
     if ($request->isXmlHttpRequest())
  {
	  if ('*' == $query || !$this->articles)
    {
      return $this->renderText('No results.');
    }
	  else {
    return $this->renderPartial('sfBlogArticles/articles', array('blog_categories' => $this->articles));
}
  }*/
  if (!$query = $request->getParameter('query'))
    {
      return $this->forward('sfBlogArticles', 'index');
    }
    

    $this->articles = Doctrine::getTable('BlogArticle')->getForLuceneQuery($query);

    if ($request->isXmlHttpRequest())
    {
      if ('*' == $query || !$this->articles)
      {
        return $this->renderText('Aucun résultat.');
      }
      else
      {
        return $this->renderPartial('sfBlogArticles/articles', array('blog_categories' => $this->articles));
      }
    }
  }
  
  
	
  public function executeIndex(sfWebRequest $request)
  {
	  $this->blog_covers =  Doctrine::getTable('BlogArticle')->getLastArticles();
   
    $this->blog_categories =  Doctrine::getTable('BlogArticle')->getActivatedArticle();
  
   $this->pager = new sfDoctrinePager(
    'BlogArticle',
    sfConfig::get('app_max_articles_by_category_page')
  );
  $this->pager->setQuery(Doctrine::getTable('BlogArticle')->getActivatedArticle2());
  $this->pager->setPage($request->getParameter('page', 1));
  $this->pager->init(); 
  }
  
  public function executeMyarticles(sfWebRequest $request)
  {
     if($this->getUser()->isAuthenticated()) {
   $this->current_user = $this->getUser()->getId();
   
 
  $this->blog_articles = Doctrine_Core::getTable('BlogArticle')->getArticleByUser($this->getUser()->getId());
    
}
else {
	  $this->forward404Unless();
  }
    
  }

  public function executeShow(sfWebRequest $request)
  {
   $this->article =  $this->getRoute()->getObject();
   
    
     $this->getUser()->addArticleHistory($this->article);
 
 
  }

  public function executeNew(sfWebRequest $request)
  {
	 if($this->getUser()->isAuthenticated()) {  
    $this->form = new BlogArticleForm();
    $this->form->setDefault('user_id', $this->getUser()->getId() );
}
else {
	  $this->forward404Unless();
  }
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new BlogArticleForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
    
    
  }

  public function executeEdit(sfWebRequest $request)
  {
	   
	  
	  
	 if ( $this->getRoute()->getObject()->getUserId() == $this->getUser()->getId()) {
    $this->form = new BlogArticleForm($this->getRoute()->getObject());
}
else {
	  $this->forward404Unless();
  }
  }

  public function executeUpdate(sfWebRequest $request)
  {
    
    $this->form = new BlogArticleForm($this->getRoute()->getObject());

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

       $article = $this->getRoute()->getObject();
  
      $article->delete();

    $this->redirect('articles');
    

    
  }
  
  public function executePublish(sfWebRequest $request)
{
  $request->checkCSRFProtection();
 
  $article = $this->getRoute()->getObject();
  $article->publish();
 if ($cache = $this->getContext()->getViewCacheManager())
  {
    $cache->remove('sfBlogArticles/index');
    $cache->remove('sfBlogArticles/index?sf_format=*');
    $cache->remove('sfBlogArticles/index?page=*&sf_format=*');
  }
   $this->getUser()->setFlash('notice', sprintf('Votre article est publié'));
 
   $this->redirect('show_articles', $article);
}

public function executePublishlater(sfWebRequest $request)
{
  $request->checkCSRFProtection();
 
  $article = $this->getRoute()->getObject();
  $article->publishlater($request->getParameter('dat'));
 $request->getParameter('date');
 
   $this->getUser()->setFlash('notice', sprintf('Votre article sera publié'));
 
   $this->redirect('show_articlestoken', $article);
}

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
	  
  
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $article = $form->save();

      $this->redirect('show_articlestoken', $article);
    }
  }
}
